package com.business.photo;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.photo.PhotoDetail;
import com.config.SystemGlobals;
import com.sql.DBFactory;
import com.util.DeleteFile;

public class PhotoDetailBus {

	private Connection conn = null;

	private Statement stmt = null;

	private static final Log log = LogFactory.getLog(PhotoDetailBus.class);

	public void add(PhotoDetail pd) {
		String photoName = pd.getPhotoName();
		String photoMemo = pd.getPhotoMemo();
		String photoUrl = pd.getPhotoUrl();
		String phototypeId = pd.getPhototypeId();
		String photoNo = pd.getPhotoNo();
		String replyCount = pd.getReplyCount();
		String viewCount = pd.getViewCount();
		String isShow = pd.getIsShow();
		String addTime = pd.getAddTime();
		String username = pd.getUsername();
		String sql = "insert into photo_detail(photo_name,photo_no,username,add_time,"
				+ "view_count,phototype_id,reply_count,photo_url,photo_memo,isShow) values('";
		sql += photoName + "','" + photoNo + "','" + username + "','" + addTime
				+ "'," + viewCount + "," + phototypeId + ",";
		sql += replyCount + ",'" + photoUrl + "','" + photoMemo + "'," + isShow
				+ ")";
		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		//	System.out.println(sql);
		} catch (Exception e) {
			log.error("the insert photodetail exception:" + e.getMessage());
			log.error("the insert photodetail sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void update(PhotoDetail pd, String photoNo) {
		String photoName = pd.getPhotoName();
		String photoMemo = pd.getPhotoMemo();
		String photoUrl = pd.getPhotoUrl();
		String phototypeId = pd.getPhototypeId();
		String replyCount = pd.getReplyCount();
		String viewCount = pd.getViewCount();
		String isShow = pd.getIsShow();
		String username = pd.getUsername();
		String sql = "update photo_detail set photo_name='" + photoName
				+ "',username='" + username + "',view_count=" + viewCount
				+ ",phototype_id=" + phototypeId + ",reply_count=" + replyCount
				+ ",photo_url='" + photoUrl + "',photo_memo='" + photoMemo
				+ "',isShow=" + isShow + " where photo_no='" + photoNo+"'";

		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
						
		} catch (Exception e) {
			log.error("the update photodetail exception:" + e.getMessage());
			log.error("the update photodetail sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void del(String photoNo) {
		String sql = "delete from photo_detail where photo_no='" + photoNo
				+ "'";
		try {
			PhotoListBus plb=new PhotoListBus();
			PhotoDetail pd=plb.getPhotoByNo(photoNo);
			String photoUrl=pd.getPhotoUrl();
			DeleteFile.delete(SystemGlobals.getValue("web_root")+photoUrl);
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			
			
			
			
		} catch (Exception e) {
			log.error("the delete photodetail exception:" + e.getMessage());
			log.error("the delete photodetail sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

}
